package com.bsoft.hospitalchs.ui.home;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bsoft.hospitalchs.base.BaseCommonPresenter;
import com.bsoft.hospitalchs.model.SearchBean;
import com.bsoft.hospitalchs.model.SearchListReq;
import com.bsoft.hospitalchs.model.SearchResBean;
import com.bsoft.hospitalchs.model.SearchResList;
import com.bsoft.hospitalchs.ui.child.ChildInfoActivity;
import com.bsoft.hospitalchs.utils.ToastUtils;
import com.google.gson.Gson;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by 泅渡者
 * Created on 2017/5/3.
 */

public class HomePresenter extends BaseCommonPresenter<HomeContract.View> implements HomeContract.Presenter {
    private SearchResBean searchResBean;
    public HomePresenter(HomeContract.View view, Context context) {
        super(view, context);
    }

    @Override
    public void toSearch(final SearchBean searchBean) {
        mCompositeSubscription.add(apiImple.doGetSearch(searchBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SearchResBean>() {
                    @Override
                    public void onCompleted() {
                        Log.e("查找失败", "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("查找失败", e.getMessage());
                    }

                    @Override
                    public void onNext(SearchResBean object) {
                     if("2000".equals(object.getCode())){
                         ToastUtils.showShort("该卡号不存在！");
                     }else{
                         searchResBean = object;
                         Log.e("xxxxxx",searchResBean.getBody().toString());
                         view.showInfo(object);
                     }
                    }
                }));
    }

    @Override
    public void toInfoActivity() {
        Intent intent = new Intent();
        intent.setClass(context,ChildInfoActivity.class);
        Gson gson = new Gson();
        String str = gson.toJson(searchResBean);
        intent.putExtra("SearchResBean",str);
        context.startActivity(intent);
    }

    @Override
    public void getActivePerson(SearchListReq searchListReq) {
        mCompositeSubscription.add(apiImple.doGetSearchList(searchListReq)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SearchResList>() {
                    @Override
                    public void onCompleted() {
                        Log.e("查找失败", "");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("查找失败", e.getMessage());
                    }

                    @Override
                    public void onNext(SearchResList object) {
                        if(object.getCode().equals("1000")){
                            view.showInfolist(object);
                        }
                        view.stopRefresh();
                    }
                }));
    }
}
